package com.huabo.system.service.impl;


import com.hbfk.entity.DealUserToken;
import com.hbfk.entity.TblStaffUtil;
import com.hbfk.util.PageInfo;
import com.huabo.system.config.DateBaseConfig;
import com.huabo.system.mysql.entity.TblRoleMySql;
import com.huabo.system.mysql.entity.TblStaffMySql;
import com.huabo.system.mysql.mapper.TblRoleMySqlDao;
import com.huabo.system.oracle.entity.TblRole;
import com.huabo.system.oracle.entity.TblStaff;
import com.huabo.system.oracle.mapper.TblRoleDao;
import com.huabo.system.service.TblRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("TblRoleService")
@Slf4j
public class TblRoleServiceImpl implements TblRoleService {

    @Resource
    private TblRoleDao tblRoleDao;

    @Resource
    private TblRoleMySqlDao tblRoleMySqlDao;


    @Override
    public List<TblRole> findAll(BigDecimal companyid) {
        return this.tblRoleDao.findBysql(companyid);
    }

    @Override
    public List<TblRoleMySql> findAllMySql(BigDecimal companyid) {
        return this.tblRoleMySqlDao.findBysql(companyid);
    }

    @Override
    public void save(TblRole tr) {
        this.tblRoleDao.saveTblRole(tr);
    }

    @Override
    public void saveMySql(TblRole tr) {
        this.tblRoleMySqlDao.saveTblRole(tr);
    }

    @Override
    public TblRole findByid(String rid) {
        List<TblRole> list = this.tblRoleDao.findByRid(rid);
        return list != null && list.size() > 0 ? (TblRole) list.get(0) : null;
    }

    @Override
    public TblRoleMySql findByMySqlid(String rid) {
        List<TblRoleMySql> list = this.tblRoleMySqlDao.findByRid(rid);
        return list != null && list.size() > 0 ? (TblRoleMySql) list.get(0) : null;
    }

    @Override
    public List<TblStaff> isSY(String rid) {
        return this.tblRoleDao.findBysqlobj(rid);
    }

    @Override
    public List<TblStaffMySql> isMySqlSY(String rid) {
        return this.tblRoleMySqlDao.findBysqlobj(rid);
    }

    @Override
    public void update(TblRole role) {
        tblRoleDao.updateTblRole(role);
    }

    @Override
    public void updateMySql(TblRole role) {
        tblRoleMySqlDao.updateTblRole(role);
    }

//    @Override
//    public void delete(TblRole role) {
//        this.tblRoleDao.deleteTblRole(role);
//
//    }

    @Override
    public Map<String, Object> findTblRoleAll(TblRole role, Integer pageNumber, Integer pageSize, String token) {
        Map<String, Object> resultMap = new HashMap<String, Object>(0);
        try {
            PageInfo<TblRole> pageInfo = new PageInfo<TblRole>();
            TblStaffUtil staff = DealUserToken.parseUserToken(token);
//        BigDecimal rid = staff.getTrole().getRid();
//        String rname =staff.getTrole().getRname();

            BigDecimal companyid = staff.getCurrentOrg().getOrgid();
            pageInfo.setCurrentPage(pageNumber);
            pageInfo.setPageSize(pageSize);
            pageInfo.setTlist(tblRoleDao.selectListByPageInfo(pageInfo, companyid, role));
            pageInfo.setTotalRecord(tblRoleDao.selectListByPageInfoCount(pageInfo, companyid));
            resultMap.put("code", "1");
            resultMap.put("msg", "访问接口成功");
            resultMap.put("data", pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultMap;
    }

    @Override
    public Map<String, Object> findMySqlTblRoleAll(TblRole role, Integer pageNumber, Integer pageSize, String token) {
        Map<String, Object> resultMap = new HashMap<String, Object>(0);
        try {
            PageInfo<TblRoleMySql> pageInfo = new PageInfo<TblRoleMySql>();
            TblStaffUtil staff = DealUserToken.parseUserToken(token);
//        BigDecimal rid = staff.getTrole().getRid();
//        String rname =staff.getTrole().getRname();

            BigDecimal companyid = staff.getCurrentOrg().getOrgid();
            pageInfo.setCurrentPage(pageNumber);
            pageInfo.setPageSize(pageSize);
            pageInfo.setTlist(tblRoleMySqlDao.selectListByPageInfo(pageInfo, companyid, role));
            pageInfo.setTotalRecord(tblRoleMySqlDao.selectListByPageInfoCount(pageInfo, companyid));
            resultMap.put("code", "1");
            resultMap.put("msg", "访问接口成功");
            resultMap.put("data", pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultMap;
    }

    @Override
    public void delete(TblRole role) {
        tblRoleDao.deleteRole(role.getRid());
    }

    @Override
    public void deleteMySql(TblRoleMySql role) {
        tblRoleMySqlDao.deleteRole(role.getRid());
    }

    @Override
    public void deleteByRid(Integer rid) {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            tblRoleDao.deleteByRid(rid);
        } else {
            tblRoleMySqlDao.deleteByRid(rid);
        }
    }

    @Override
    public Boolean findIsByJs(String jsmc, TblStaff staff) {
        // TODO Auto-generated method stub
        return null;

    }

    @Override
    public Boolean findMySqlIsByJs(String jsmc, TblStaffMySql staff) {
        // TODO Auto-generated method stub
        return null;
    }

//    @Override
//    public void delete(BigDecimal rid) {
//        tblRoleDao.deleteRid(rid);
//    }
//
}
